<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/15 0015
 * Time: 17:32
 */

namespace com;

use PHPMailer\PHPMailer\PHPMailer;
use think\Exception;

class EmailUtils
{

    static function send($to,$toName,$subject,$html,$controller){

        if(!array_key_exists('email_service_host',$controller->mipInfo)){
            throw new Exception("请登录管理后台 >> 电子邮箱服务设置，设置电子邮箱服务信息 ");
        }

        $mail = new PHPMailer(true);
        $mail->IsSMTP(); // 启用SMTP
        $mail->Host = $controller->mipInfo['email_service_host'];
        $mail->Port = $controller->mipInfo['email_service_port'];  //邮件发送端口
        $mail->SMTPAuth = true;  //启用SMTP认证
        $mail->SMTPSecure = "ssl";   // 设置安全验证方式为ssl
        $mail->CharSet = "UTF-8"; //字符集
        $mail->Encoding = "base64"; //编码方式
        $mail->Username = $controller->mipInfo['email_username'];  //你的邮箱
        $mail->Password = $controller->mipInfo['email_auth_passwd'];  //你的密码
        $mail->Subject = $subject; //邮件标题
        $mail->From = $controller->mipInfo['email_username'];  //发件人地址（也就是你的邮箱）
        $mail->FromName = $controller->mipInfo['email_name'];  //发件人姓名

        $mail->AddAddress($to, $toName); //添加收件人（地址，昵称）
        $mail->IsHTML(true); //支持html格式内容
        $mail->Body = $html; //邮件主体内容
        //发送成功就删除
        if ($mail->Send()) {
            return true;
        }else{
            throw new Exception($mail->ErrorInfo);// 输出错误信息
        }

    }


}